<?php
class work extends head { 
 function work() {
  $this->table = "work";
  $this->getmetadata();
  $this->maintainhist = false;
  $this->header[2] = "";
  $_SESSION[strtoupper($this->table)] = $this;
  $_SESSION["work"] =$this;
 }
 
 function applyrefs() {
  parent::applyrefs();
  $this->refs[5] = freadfield("employee",$this->header[5],"name");
  $this->refs[6] = freadfield("process",$this->header[6],"name");
  $this->refs[7] = freadfield("pgroup",$this->header[7],"name");
  $this->refs[8] = freadfield("jobs",$this->header[8],"name");  
 }
 
 function createblank() {
  $this->header = array_fill(0,$this->atts,"");
  $this->header[2] = "0";
  $this->valids = array_fill(0,$this->atts,"");
  $this->lists = array_fill(0,$this->atts,"");
  $this->refs = array_fill(0,$this->atts,"");  
  $this->header[18] = dtoiso("");
  $this->header[11] = "0000-00-00 00:00:00";
  $this->header[12] = "0000-00-00 00:00:00";
  $this->header[13] = '0';
  $user = authenticate(0);
 }
 
  function selectlist($condition) {
  // Last search cached for speed.
  if ($condition == $this->lastsearch) {
    return $this->lastsearchresults;
  } else {
	  $stmt = "select itemid,name,jobid,processid,starttime,endtime from ".$this->table;
	  $words = explode(" ",$condition);
	  if ($condition !=="" and $condition !=="ALL") {
	   if (sizeof($words > 0)) {
	    $stmt .= " where CONCAT(name,processid,starttime) like '%$words[0]%'";
	    for ($i=1;$i<sizeof($words);$i++) {
	     $stmt .= " and CONCAT(name,processid,starttime) like '%$words[$i]%'";
	    }
	   } else {
	    $stmt .= " where CONCAT(name,processid,starttime) like '%$condition%'";
	   }
	  }
	  $stmt .= " order by editdate desc limit 1000";
	  $ans = sqlreadarray($stmt);
	  $this->lastsearchresults = $ans;
	  $this->lastsearch = $condition;
    return $ans;
  }
 }
 
 function applyfromrequest() {
  // Mapped requests F1-> field 1 etc, L1->Line 1 etc
  
  $this->valid = true;
  $this->valids = array_fill(0,$this->atts,"");  
  $this->lists = array_fill(0,$this->atts,"");


  $i = 5; $t = "employee";
	if ($f=@$_REQUEST["f$i"]) {
   // Employee object id
   if ($f !== $this->header[$i]) {
    $desc = freaditem($t,$f);
    if ($desc == "") {
     $this->valids[$i] = "Need valid $t code";
     $this->lists[$i] = $this->selectlisth($t,$f,"f$i");
    } else {
     $this->header[$i] = $f;
     $this->refs[$i] = $desc[1];
     $this->header[1] = $desc[1];
    }
   }
  }

  $i = 6; $t = "process";
	if ($f=@$_REQUEST["f$i"]) {
   // Process object id
   if ($f !== $this->header[$i]) {
    $desc = freaditem($t,$f);
    if ($desc == "") {
     $this->valids[$i] = "Need valid $t code";
     $this->lists[$i] = $this->selectlisth($t,$f,"f$i");
    } else {
     $this->header[$i] = $f;
     $this->refs[$i] = $desc[1];
     $this->lists[$i] = "";
    }
   }
  }

  $i = 7; $t = "pgroup";
	if ($f=@$_REQUEST["f$i"]) {
   // Product Group object id
   if ($f !== $this->header[$i]) {
    $desc = freaditem($t,$f);
    if ($desc == "") {
     $this->valids[$i] = "Need valid $t code";
     $this->lists[$i] = $this->selectlisth($t,$f,"f$i");
    } else {
     $this->header[$i] = $f;
     $this->refs[$i] = $desc[1];
     $this->lists[$i] = "";
    }
   }
  }

  $i = 8; $t = "jobs";
	if ($f=@$_REQUEST["f$i"]) {
   // Jobs object id
   if ($f !== $this->header[$i]) {
    $desc = freaditem($t,$f);
    if ($desc == "") {
     $this->valids[$i] = "Need valid job code";
     $this->lists[$i] = $this->selectlisth($t,$f,"f$i");
    } else {
     $this->header[$i] = $f;
     $this->refs[$i] = $desc[1];
     $this->lists[$i] = "";
    }
   }
  }
   
  $i = 9;
	if ($f=@$_REQUEST["f$i"]) {
	 if (preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/",$f)) {
 	  $this->header[$i] = $f;
	 } else {
	  $this->valids[$i] = "Wrong format use YYYY-MM-DD HH:MM:SS eg 2009-12-31 15:15:00";
	 }
  }
	  
  $i = 10;
	if ($f=@$_REQUEST["f$i"]) {
	 if (preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/",$f)) {
 	  $this->header[$i] = $f;
	 } else {
	  $this->valids[$i] = "Wrong format use YYYY-MM-DD HH:MM:SS eg 2009-12-31 15:15:00";
	 }
  }

  $i = 11;
	if ($f=@$_REQUEST["f$i"]) {
	 if (preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/",$f)) {
 	  $this->header[$i] = $f;
	 } else {
	  $this->valids[$i] = "Wrong format use YYYY-MM-DD HH:MM:SS eg 2009-12-31 15:15:00";
	 }
  }
  
  $i = 12;
	if ($f=@$_REQUEST["f$i"]) {
	 if (preg_match("/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/",$f)) {
 	  $this->header[$i] = $f;
	 } else {
	  $this->valids[$i] = "Wrong format use YYYY-MM-DD HH:MM:SS eg 2009-12-31 15:15:00";
	 }
  }

  if ($this->header[5] == "") {
   $this->valids[5] = "Cannot leave employee blank";
  }
  if ($this->header[6] == "") {
   $this->valids[6] = "Cannot leave process blank";
  }
  if (strtotime($this->header[10]) < strtotime($this->header[9])) {
   $this->valids[9] = "Start time must be before end time";
  }

 }
 
 function newcounter() {
  return "0";
 }
  
 function showform() {
  $focalpoint = "f5";
  echo "<head>";
  echo "<script src='datechooser.js' type='text/javascript'></script>";
  echo SCRIPTS ;
  //
  echo "</head>";
	echo "<body onload='document.form.".$focalpoint.".focus()'>";
	echo "<h1>Manage Work Items</h1>";
  echo "<form name=form method=POST>";
  echo "<input type=hidden name=res value=''>";
  echo "<table>";
  echo $this->htmlrow(5,"Employee");
  echo $this->htmlrow(6,"Process");
  echo $this->htmlrow(7,"Product Group");
  echo $this->htmlrow(8,"Job");
  echo $this->htmlrow(9,"Start Time");
  echo $this->htmlrow(10,"End Time");
  echo $this->htmlrow(11,"Schedule Start Time");
  echo $this->htmlrow(12,"Schedule End Time");
  echo "</table>";
  echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
  echo "<input class=bt accesskey=S type=submit name=sub value=Save>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>"; 

  echo "</form>"; 
  // set up help
  echo($this->registerhelp());
 }

}
?>